package me.zhengjie.modules.website.service.impl;

import me.zhengjie.modules.website.domain.DocumentFile;
import me.zhengjie.modules.website.repository.DocumentFileRepository;
import me.zhengjie.modules.website.repository.DocumentFileSpecifications;
import me.zhengjie.modules.website.service.DocumentFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import me.zhengjie.modules.website.domain.PageResult;

import java.util.List;

/**
 * @author system
 * @since 2024-11-06 15:51:43
 */
@Service
public class DocumentFileServiceImpl implements DocumentFileService {

    @Autowired
    private DocumentFileRepository documentFileRepository;

    @Override
    public PageResult<DocumentFile> findPage(DocumentFile documentFile, Pageable pageable) {
        Page<DocumentFile> page = documentFileRepository.findAll(DocumentFileSpecifications.queryList(documentFile), pageable);
        PageResult<DocumentFile> result = new PageResult();
        result.setContent(page.getContent());
        result.setTotalElements(page.getTotalElements());
        return result;
    }

    @Override
    public List<DocumentFile> findList(DocumentFile documentFile) {
        return documentFileRepository.findAll(DocumentFileSpecifications.queryList(documentFile));
    }

    @Override
    public DocumentFile findById(Long id) {
        return documentFileRepository.findById(id).orElse(null);
    }

    @Override
    public DocumentFile insert(DocumentFile documentFile) {
        return documentFileRepository.save(documentFile);
    }

    @Override
    public DocumentFile update(DocumentFile documentFile) {
        return documentFileRepository.saveAndFlush(documentFile);
    }

    @Override
    public void delete(Long id) {
        documentFileRepository.deleteById(id);
    }

}
